package Agency.models;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import Agency.views.*;
//import Agency.models;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author wangari
 */
public class LoginController extends javax.swing.JFrame{
     public  LoginView  myLoginView ;
    Connection conn = null;
    private ActionListener  actionlistener;
//    private TenantsView   theTenantView;
    
  /**
   * Constructor  to  create a new object of the login view
   */  
    public LoginController(){
      myLoginView = new LoginView();  
     myLoginView .setVisible(true);
//      theTenantView = new TenantsView ();
    }
    /**
     * method to log out. it closes the login view
     */
    
    public void loggingOut(){
      myLoginView .setVisible(false);   
    }
  /**
   * Validating the username and the password
   * if the password and the username is correct, it allows the user to log in if either password
   * or username are wrong it prompts to log in again
   */  
 public void loginController( TenantsView   theTenantView){
     
      String   theUsername = myLoginView .getUserName().getText(); 
      String   thePassword =  myLoginView.getPassword().getText();
      System.out.println(thePassword);
String loginUsername="";
char[] loginPassword;
ArrayList<String> myUsernameArray = new ArrayList<String>();
ArrayList<char[]> myPasswordArray = new ArrayList<char[]>();
  try{
      System.out.println("connected to database");
      
       Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection(
 "jdbc:mysql://localhost/MusyiAgency?user=root&password=");
 String querry=("SELECT Username,CaretakerPassword  FROM Caretaker WHERE Username like '%"+ theUsername +"%' AND CaretakerPassword like'%"+thePassword+"%';");
 PreparedStatement p=conn.prepareStatement(querry);
 //p.execute();
 java.sql.ResultSet r = p.executeQuery (querry);
 // if the password or username is wrong
// if( !r/.next()){
     JOptionPane.showMessageDialog (null, "Wrong username or Password" , "Login Details", JOptionPane.INFORMATION_MESSAGE);
    
// }
// //when username and password are correct
// else{
     while(r.next()){
         if(theUsername.equals(r.getString("Username"))&& (thePassword.equals(r.getString("CaretakerPassword")))){
             theTenantView.setVisible(true); 
            JOptionPane.showMessageDialog (null, "You have succefully logged in" , "Login Details", JOptionPane.INFORMATION_MESSAGE); 
         }
         
         else{
         JOptionPane.showMessageDialog (null, "Wrong username or Password" , "Login Details", JOptionPane.INFORMATION_MESSAGE);
         }
//     }
 }
 
   
    }catch(Exception e){
        JOptionPane.showMessageDialog (null,e);
              
    }
 }
 
 
     
}
